package dshop.action.admin;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import appbase.jfinal.ext.ApiBaseController;
import appbase.jfinal.ext.ModelKit;
import appbase.jfinal.ext.RecordKit;
import dshop.AppConst;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;
import dshop.model.Area;
import dshop.model.commerce.Commerce;
import dshop.model.commerce.CommerceNews;
import dshop.model.company.Company;
import dshop.model.company.CompanyNews;
import dshop.model.member.MemberGeneralInfo;
import dshop.model.member.MemberNews;
import dshop.model.member.MemberVip;
import dshop.model.news.News;
import dshop.model.procurement.Procurement;

/**
 * 风采管理 <br>
 * 个人、公司、商会
 */
@Before({ ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class })
public class NewsAction extends ApiBaseController {

	/**
	 * 个人风采
	 */
	public void listOfMember() {
		String keyWord = getPara("keyWord", "").trim();
		int checkStatus = getParaToInt("checkStatus", -1);

		// 排序
		String columnProp = getPara("columnProp", "").trim();
		String columnOrder = getPara("columnOrder", "").trim().split("ending")[0];

		String select = " select n.id, n.account_id, n.title, n.intro, n.cover, n.url,n.news_detail, n.date_time, n.status, "
				+ " concat(m.last_name, m.first_name) as real_name, m.phone ";
		String where = " from member_news n " + " inner join member_general_info m on m.account_id = n.account_id "
				+ " where 1 = 1  ";
		String orderBy = "";

		List<Object> params = new ArrayList<Object>();
		if (!keyWord.isEmpty()) {
			where += " and (n.title like ? or m.phone like ? or m.first_name like ?)";
			String keyWordParam = "%" + keyWord + "%";
			params.add(keyWordParam);
			params.add(keyWordParam);
			params.add(keyWordParam);
		}
		if (checkStatus >= 0) {
			where += " and status = ? ";
			params.add(checkStatus);
		}
		if (!columnOrder.isEmpty()) {
			orderBy = " order by " + columnProp + " " + columnOrder;
		} else {
			orderBy = " ORDER BY n.date_time desc";
		}

		Page<Record> page = page(select, where + orderBy, params.toArray());
		List<Record> list = page.getList();

		RecordKit.putDateStr(list, "date_time", "date_time", "yyyy-MM-dd HH:mm");
		convertCoverToList(list, "cover", "coverList");
		convertCoverToList(list, "news_detail", "newsDetailList");

		renderSuccess();
	}

	public void delete() {
		int newsId = getParaToInt("id");

		News.delete(newsId);

		renderSuccess("删除成功");
	}

	public void checkStatusPass() {
		int newId = getParaToInt("id");

		News.setCheckStatus(newId, "", News.STRUTS_NORMAL);

		renderSuccess("成功设置为已通过");
	}

	public void checkStatusFail() {
		int newId = getParaToInt("id");
		String checkStatusExplain = getPara("checkStatusExplain", "").trim();

		News.setCheckStatus(newId, checkStatusExplain, News.STRUTS_FAIL);

		renderSuccess("已设置为不通过");
	}

	public void detailOfMember() {
		int newsId = getParaToInt("id");

		MemberNews news = MemberNews.dao.findById(newsId);
		setAttr("news", news);
		
		int accountId = news.getInt("account_id");
		news.put("account", MemberGeneralInfo.dao.findById(accountId));
		news.put("company", Company.getCompanyInfoByAccountId(accountId));


		ModelKit.putDateFormat(news, "date_time", "date_time_str", "yyyy-MM-dd HH:mm");
		convertCoverToList(news, "cover", "coverList");
		convertCoverToList(news, "news_detail", "newsDetailList");

//		MemberGeneralInfo member = MemberGeneralInfo.dao.findById(accountId);
//		setAttr("member", member);
//
//		MemberVip memberVip = MemberVip.dao.findById(accountId);
//		if (memberVip == null) {
//			memberVip = new MemberVip();
//		}
//		setAttr("memberVip", memberVip);
//		ModelKit.putDateFormat(memberVip, "start_date_time", "start_date_time_str", "yyyy-MM-dd HH:mm");
//		ModelKit.putDateFormat(memberVip, "end_date_time", "end_date_time_str", "yyyy-MM-dd HH:mm");

		renderSuccess();
	}

	private <M extends Model<M>> void convertCoverToList(M news, String coverColumn, String coverListColumn) {
		String cover = news.getStr(coverColumn);
		List<String> coverList = JSON.parseArray(cover).toJavaList(String.class);
		news.put(coverListColumn, coverList);
	}

	private void convertCoverToList(List<Record> list, String coverColumn, String coverListColumn) {
		if (list == null || list.isEmpty()) {
			return;
		}

		for (Record r : list) {
			String cover = r.getStr(coverColumn);
			if (cover == null || cover.isEmpty()) {
				return;
			}
			List<String> coverList = JSON.parseArray(cover).toJavaList(String.class);
			r.set(coverListColumn, coverList);
		}
	}

	/**
	 * 公司风采
	 */
	public void listOfCompany() {
		String keyWord = getPara("keyWord", "").trim();

		// 排序
		String columnProp = getPara("columnProp", "").trim();
		String columnOrder = getPara("columnOrder", "").trim().split("ending")[0];

		String select = " select n.id, n.account_id, n.company_id, n.title, n.intro, n.cover, n.url, n.date_time, "
				+ " c.name as company_name, c.company_tel, c.intro as company_intro, c.address as company_address ";
		String where = " from company_news n " + " inner join company c on n.company_id = c.id " + " where 1 = 1  ";
		String orderBy = " ";

		List<Object> params = new ArrayList<Object>();
		if (!keyWord.isEmpty()) {
			where += " and (n.title like ? or n.intro like ? or c.name like ?)";
			String keyWordParam = "%" + keyWord + "%";
			params.add(keyWordParam);
			params.add(keyWordParam);
			params.add(keyWordParam);
		}
		if (!columnOrder.isEmpty()) {
			orderBy += "order by " + columnProp + " " + columnOrder;
		} else {
			orderBy = "";
		}

		Page<Record> page = page(select, where + orderBy, params.toArray());
		List<Record> list = page.getList();

		RecordKit.putDateStr(list, "date_time", "date_time_str", "yyyy-MM-dd HH:mm");
		convertCoverToList(list, "cover", "coverList");

		renderSuccess();
	}

	public void detailOfCompany() {
		int newsId = getParaToInt("id");

		CompanyNews news = CompanyNews.dao.findById(newsId);
		setAttr("news", news);

		int companyId = news.getInt("company_id");

		ModelKit.putDateFormat(news, "date_time", "date_time_str", "yyyy-MM-dd HH:mm");
		convertCoverToList(news, "cover", "coverList");

		Company company = Company.dao.findById(companyId);
		Area.setNameToM(company, "province_code, city_code, area_code", "province, city, area");
		setAttr("company", company);

		renderSuccess();
	}

	/**
	 * 商会风采
	 */
	public void listOfCommerce() {
		String keyWord = getPara("keyWord", "").trim();

		// 排序
		String columnProp = getPara("columnProp", "").trim();
		String columnOrder = getPara("columnOrder", "").trim().split("ending")[0];

		String select = " select n.id, n.account_id, n.commerce_id, n.title, n.intro, n.cover, n.url, n.date_time, "
				+ " c.name as commerce_name, c.address as commerce_address ";
		String where = " from commerce_news n " + " inner join commerce c on n.commerce_id = c.id " + " where 1 = 1  ";
		String orderBy = " ";

		List<Object> params = new ArrayList<Object>();
		if (!keyWord.isEmpty()) {
			where += " and (n.title like ? or n.intro like ? or c.name like ?)";
			String keyWordParam = "%" + keyWord + "%";
			params.add(keyWordParam);
			params.add(keyWordParam);
			params.add(keyWordParam);
		}
		if (!columnOrder.isEmpty()) {
			orderBy += "order by " + columnProp + " " + columnOrder;
		} else {
			orderBy = "";
		}

		Page<Record> page = page(select, where + orderBy, params.toArray());
		List<Record> list = page.getList();

		RecordKit.putDateStr(list, "date_time", "date_time_str", "yyyy-MM-dd HH:mm");
		convertCoverToList(list, "cover", "coverList");

		renderSuccess();
	}

	public void detailOfCommerce() {
		int newsId = getParaToInt("id");

		CommerceNews news = CommerceNews.dao.findById(newsId);
		setAttr("news", news);

		ModelKit.putDateFormat(news, "date_time", "date_time_str", "yyyy-MM-dd HH:mm");
		convertCoverToList(news, "cover", "coverList");

		int accountId = news.getInt("account_id");
		int commerceId = news.getInt("commerce_id");

		Commerce commerce = Commerce.dao.findById(commerceId);
		Area.setNameToM(commerce, "province_code, city_code, area_code", "province, city, area");
		setAttr("commerce", commerce);
		convertCoverToList(commerce, "cover", "coverList");

		MemberGeneralInfo member = MemberGeneralInfo.dao.findById(accountId);
		setAttr("member", member);

		MemberVip memberVip = MemberVip.dao.findById(accountId);
		if (memberVip == null) {
			memberVip = new MemberVip();
		}
		setAttr("memberVip", memberVip);
		ModelKit.putDateFormat(memberVip, "start_date_time", "start_date_time_str", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(memberVip, "end_date_time", "end_date_time_str", "yyyy-MM-dd HH:mm");

		renderSuccess();
	}

}
